from flask import Blueprint, request, jsonify
from app.models.Skillc2 import Bonus, db

bonus_bp = Blueprint('bonus', __name__)

@bonus_bp.route('/bonus', methods=['GET'])
def get_bonus_list():
    """获取奖励列表"""
    try:
        page = request.args.get('page', 1, type=int)
        per_page = request.args.get('per_page', 10, type=int)
        
        # 获取激活的奖励
        pagination = Bonus.query.filter_by(IsActive=True).paginate(
            page=page, per_page=per_page, error_out=False)
        
        items = [item.to_dict() for item in pagination.items]
        
        data = {
            'items': items,
            'total': pagination.total,
            'pages': pagination.pages,
            'current_page': pagination.page,
            'has_next': pagination.has_next,
            'has_prev': pagination.has_prev
        }
        
        return jsonify({'status': 'success', 'message': '获取奖励列表成功', 'data': data})
    except Exception as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400

@bonus_bp.route('/bonus/<int:bonus_id>', methods=['GET'])
def get_bonus_detail(bonus_id):
    """获取奖励详情"""
    try:
        bonus = Bonus.query.get_or_404(bonus_id)
        return jsonify({'status': 'success', 'message': '获取奖励详情成功', 'data': bonus.to_dict()})
    except Exception as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400

@bonus_bp.route('/bonus', methods=['POST'])
def create_bonus():
    """创建奖励"""
    try:
        data = request.get_json()
        title = data.get('title')
        image = data.get('image')
        points = data.get('points')
        description = data.get('description')
        
        if not all([title, points]):
            return jsonify({'status': 'error', 'message': '标题和积分不能为空'}), 400
        
        bonus = Bonus(
            Title=title,
            Image=image,
            Points=points,
            Description=description
        )
        
        db.session.add(bonus)
        db.session.commit()
        
        return jsonify({'status': 'success', 'message': '创建奖励成功', 'data': bonus.to_dict()})
    except Exception as e:
        db.session.rollback()
        return jsonify({'status': 'error', 'message': str(e)}), 400

@bonus_bp.route('/bonus/<int:bonus_id>', methods=['PUT'])
def update_bonus(bonus_id):
    """更新奖励"""
    try:
        bonus = Bonus.query.get_or_404(bonus_id)
        data = request.get_json()
        
        if 'title' in data:
            bonus.Title = data['title']
        if 'image' in data:
            bonus.Image = data['image']
        if 'points' in data:
            bonus.Points = data['points']
        if 'description' in data:
            bonus.Description = data['description']
        if 'is_active' in data:
            bonus.IsActive = data['is_active']
        
        db.session.commit()
        return jsonify({'status': 'success', 'message': '更新奖励成功', 'data': bonus.to_dict()})
    except Exception as e:
        db.session.rollback()
        return jsonify({'status': 'error', 'message': str(e)}), 400

@bonus_bp.route('/bonus/<int:bonus_id>', methods=['DELETE'])
def delete_bonus(bonus_id):
    """删除奖励"""
    try:
        bonus = Bonus.query.get_or_404(bonus_id)
        bonus.IsActive = False
        db.session.commit()
        return jsonify({'status': 'success', 'message': '删除奖励成功'})
    except Exception as e:
        db.session.rollback()
        return jsonify({'status': 'error', 'message': str(e)}), 400 